package com.tb.market;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.tb.coremodel.datamodel.http.entities.MarketData;
import com.tb.coremodel.viewmodel.MarketViewModel;
import com.tb.market.databinding.MarketItemBinding;

import java.util.List;

/**
 * Created by Clark on 2022/02/28.
 */

public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.MarketViewHolder> {

    List<MarketData.ResultsBean> marketList;
    MarketItemClickCallback marketItemClickCallback;

    /**
     * 构造方法传入点击监听器
     * @param itemClickCallback
     */
    public MarketAdapter(@Nullable MarketItemClickCallback itemClickCallback) {
        marketItemClickCallback = itemClickCallback;
    }

    public void setMarketList(final List<MarketData.ResultsBean> list){
        if(marketList == null){
            marketList = list;
            notifyItemRangeInserted(0, marketList.size());
        }else {
            DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() {
                @Override
                public int getOldListSize() {
                    return marketList.size();
                }

                @Override
                public int getNewListSize() {
                    return list.size();
                }

                @Override
                public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                    MarketData.ResultsBean oldData = marketList.get(oldItemPosition);
                    MarketData.ResultsBean newData = list.get(newItemPosition);
                    return oldData.get_id() == newData.get_id();
                }

                @Override
                public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                    MarketData.ResultsBean oldData = marketList.get(oldItemPosition);
                    MarketData.ResultsBean newData = list.get(newItemPosition);
                    return oldData.get_id() == newData.get_id()
                            && oldData.getCreatedAt() == newData.getCreatedAt()
                            && oldData.getPublishedAt() == newData.getPublishedAt()
                            && oldData.getSource() == newData.getSource();
                }
            });
            marketList = list;
            diffResult.dispatchUpdatesTo(this);
        }
    }

    @Override
    public MarketViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MarketItemBinding binding = DataBindingUtil
                .inflate(LayoutInflater.from(parent.getContext()), R.layout.market_item,
                        parent, false);
        binding.setCallback(marketItemClickCallback);
        return new MarketViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(MarketViewHolder holder, int position) {
        holder.binding.setMarketItem(marketList.get(position));
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return marketList == null ? 0 : marketList.size();
    }

    static class MarketViewHolder extends RecyclerView.ViewHolder {
        MarketItemBinding binding;
        public MarketViewHolder(MarketItemBinding binding ) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
